//
//  EditViewController.m
//  Daybook
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "EditViewController.h"
#import "Common.h"

@interface EditViewController ()
<
 EditViewDelegate,
 UIActionSheetDelegate,
 UIImagePickerControllerDelegate,
 UINavigationControllerDelegate
>
@property (nonatomic, strong) EditView *editView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *completeButton;
@property (nonatomic, strong) UIImage *image;
@end

@implementation EditViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
   self.navigationController.navigationBarHidden = NO;
    self.navigationItem.hidesBackButton = YES;
    _backButton.hidden = NO;
    _completeButton.hidden = NO;
    if (self.picImage) {
        _editView.image = self.picImage;
    }
    if (self.remarks) {
        _editView.remarks = self.remarks;
    }
    

    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.navigationItem.hidesBackButton = YES;
    _backButton.hidden = YES;
    _completeButton.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    
    [self createView];
}
- (void)createView {
    self.navigationItem.title = @"备注";
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(MAIN_WIDTH / 30, self.navigationController.navigationBar.frame.size.height / 4, self.navigationController.navigationBar.frame.size.height / 2, self.navigationController.navigationBar.frame.size.height / 2);
    //_backButton.backgroundColor = [UIColor redColor];
    [_backButton setImage:[UIImage imageNamed:@"cha.png"] forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:_backButton];
    [_backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.completeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_completeButton setImage:[UIImage imageNamed:@"duihao.png"] forState:UIControlStateNormal];
    _completeButton.frame = CGRectMake(MAIN_WIDTH - self.navigationController.navigationBar.frame.size.height / 2 - MAIN_WIDTH / 30 - 5, self.navigationController.navigationBar.frame.size.height / 4 - 3, self.navigationController.navigationBar.frame.size.height / 2 + 5, self.navigationController.navigationBar.frame.size.height / 2 + 5);
    [self.navigationController.navigationBar addSubview:_completeButton];
    //_completeButton.backgroundColor = [UIColor redColor];
    
    [_completeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.editView = [[EditView alloc] initWithFrame:self.view.frame];
    _editView.delegate = self;
    [self.view addSubview:_editView];
}
- (void)click {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"从相册中选择" otherButtonTitles:@"拍照", nil];
    sheet.delegate = self;
    [sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:^{
            
            
        }];
    }
    else {
        BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];//判断照相机是否可用（是否有摄像头）
        if(hasCamera == YES)
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.delegate = self;
            picker.allowsEditing = YES; //是否可编辑
            [self presentModalViewController:picker animated:YES];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"ok!" otherButtonTitles:nil];
            [alert show];
        }

    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{
        self.image = info[UIImagePickerControllerOriginalImage];
        [_editView.photoButton setImage:_image forState:UIControlStateNormal];
        [_editView.textView becomeFirstResponder];
    }];
}
- (void)buttonAction:(UIButton *)button {
    
    if (button == _completeButton) {
        if (_editView.textView.text.length <= 40) {
            if (_image == nil) {
                _image = _picImage;
            }
            [self.delegate sendpic:_image remarks:_editView.textView.text];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你太大了, 人家装不下" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [alert show];
        }
        
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
